#!/bin/bash

#
# (c) 2009 Mandriva, http://www.mandriva.com/
# (c) 2009 Jean-Dominique Frattini, jd.frattini@free.fr
#
# $Id: $
#
# This file is part of ATS (Automated Testing System), 
# http://http://wiki.mandriva.com/en/Automated_testing_system
#
# ATS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# ATS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

# usage: testcloud-vm-halt.sh <TIMELIMIT> <COMMAND>

KILL_CMD=`which kill`
TIME_LIMIT=5
TIMER_PID=0
CMD_PID=0

TimerOn()
{
   sleep ${TIME_LIMIT} && ${KILL_CMD} -s SIGALRM $$ &
   TIMER_PID=$!
}

Int14Vector()
{
   echo " Timed out !"
   halt
   exit
}

trap Int14Vector SIGALRM

echo "Enter 'login' within ${TIME_LIMIT} seconds to prevent the system from halting"
TimerOn

while true
do
   read stuff

   if [ "${stuff}" = "login" ]
   then
      kill ${TIMER_PID}
      echo "Login asked."
      /bin/bash
      exit 0
   fi
done

exit 0
